package com.sanery.service.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.ibatis.session.SqlSession;

import com.sanery.dao.MyBatisUtil;

public class TranAdvice implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation inv) throws Throwable {
		SqlSession session = MyBatisUtil.getSession();
		try {
			Object o = inv.proceed();
			session.commit();// 提交事务
			return o;
		} catch (Exception e) {
			session.rollback(); // 有异常说明有问题，回滚
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSession(); // 关闭
		}
	}
}
